<#-- Relationship Loop - 1:M -->
<#macro OTMRelationship theEntity>
<#list util.getOneToManyRelationships(theEntity) as rel>
	<#assign rel = rel><#-- neccessary for macros -->	
	<#nested>
</#list>
</#macro>

<#-- Relationship Loop - 1:1 -->
<#macro OTORelationship theEntity>
<#list util.getOneToOneRelationships(theEntity) as rel>
	<#assign rel = rel><#-- neccessary for macros -->	
	<#nested>
</#list>
</#macro>

<#-- Handles the formating of the entity -->
<#macro NestedPermissionFormat sourceEntity, targetEntity, level>
	<h:outputText escape="false" value="${util.repeat("&nbsp;&nbsp;", level)}${targetEntity.displayName}"/>
	<h:panelGroup>
		<#-- View -->
		<h:outputLabel for="${sourceEntity.entityName}_${targetEntity.entityName}_view" value="View:"/>
		<h:selectBooleanCheckbox id="${sourceEntity.entityName}_${targetEntity.entityName}_view" styleClass="tab${tabcounter}view" rendered="<@hb>roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.view']</@hb>" />
		<h:selectBooleanCheckbox disabled="true" rendered="<@hb>!roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.view']</@hb>" />						
	</h:panelGroup>
	
	<h:panelGroup>
		<#-- Edit -->
		<h:outputLabel for="${sourceEntity.entityName}_${targetEntity.entityName}_edit" value="Edit:"/>
		<h:selectBooleanCheckbox id="${sourceEntity.entityName}_${targetEntity.entityName}_edit" styleClass="tab${tabcounter}edit" rendered="<@hb>roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.edit']</@hb>" />
		<h:selectBooleanCheckbox disabled="true" rendered="<@hb>!roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.edit']</@hb>" />
	</h:panelGroup>
		
	<h:panelGroup>						
		<#-- Add -->
		<h:outputLabel for="${sourceEntity.entityName}_${targetEntity.entityName}_add" value="Add:"/>
		<h:selectBooleanCheckbox id="${sourceEntity.entityName}_${targetEntity.entityName}_add" styleClass="tab${tabcounter}add" rendered="<@hb>roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.add']</@hb>" />
		<h:selectBooleanCheckbox disabled="true" rendered="<@hb>!roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.add']</@hb>" />
	</h:panelGroup>
						
	<h:panelGroup>							
		<#-- Delete -->
		<h:outputLabel for="${sourceEntity.entityName}_${targetEntity.entityName}_delete" value="Delete:"/>
		<h:selectBooleanCheckbox id="${sourceEntity.entityName}_${targetEntity.entityName}_delete" styleClass="tab${tabcounter}del" rendered="<@hb>roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.delete']</@hb>" />
		<h:selectBooleanCheckbox disabled="true" rendered="<@hb>!roleHandler.edit</@hb>" value="<@hb>roleHandler.current.permissions['entity.${targetEntity.entityName}.delete']</@hb>" />	
	</h:panelGroup>
</#macro>

<#macro NestedPermissions sourceEntity, targetEntity, level>
	<@isNotAbstract sourceEntity>
		<#-- format this pair -->
		<@NestedPermissionFormat sourceEntity, targetEntity, level />
		
		<#-- examine targetEntity for any 1:M rels -->
		<@OTMRelationship targetEntity>
			<@NestedPermissions targetEntity, rel.toEntity, level + 1 />
		</@OTMRelationship>
		
		<#-- examine targetEntity for any 1:1 rels -->
		<@OTORelationship targetEntity>
			<@NestedPermissions targetEntity, rel.toEntity, level + 1 />
		</@OTORelationship>
	</@isNotAbstract>
</#macro>